package com.gopay.bis.khpay.ebank.operation;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.axis.utils.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ecc.emp.data.IndexedCollection;
import com.ecc.emp.data.KeyedCollection;
import com.gopay.common.domain.bank.khpay.ebank.KhPayMessage;
import com.sdb.payclient.core.PayclientInterfaceUtil;

/**
 * 平安跨行支付每日对账单查询接口
 * 
 * @KhPayQueryTaskThread.java
 * @author zhengxue
 * @2017-11-21 上午10:16:26 www.gopay.com.cn Inc.All rights reserved.
 */
public class KhPayQueryTaskThread implements Runnable {
    protected Logger log = LoggerFactory.getLogger(KhPayQueryTaskThread.class);
    /** 对账明细txt存储路径. */
    private String filePath = "/usr/local/ciecc/webapps/upload/KHPAY/";

    private KhPayMessage message;

    public KhPayQueryTaskThread(KhPayMessage msg) {
        this.message = msg;
    }

    @Override
    public void run() {
        FileWriter fw = null;
        try {
            log.error("KhPayQueryTaskThread req: " + ToStringBuilder.reflectionToString(message));

            PayclientInterfaceUtil util = new PayclientInterfaceUtil();
            KeyedCollection input = new KeyedCollection("input");
            KeyedCollection output = new KeyedCollection("output");
            input.put("masterId", message.getMasterId());
            input.put("date", message.getOrderDate());

            output = util.execute(input, message.getTransCode());
            String errorCode = (String) output.getDataValue("errorCode");
            String errorMsg = (String) output.getDataValue("errorMsg");

            if (StringUtils.isEmpty(errorCode) && StringUtils.isEmpty(errorMsg)) {
                // 查询成功
                String sumCount = String.valueOf(output.getDataValue("sumCount"));
                String sumAmount = String.valueOf(output.getDataValue("sumAmount"));
                log.error("KhPayQueryTaskThread query success, sumCount: " + sumCount + " sumAmount: " + sumAmount);

                IndexedCollection orderList = (IndexedCollection) output.getDataElement("iOrderListDetail");
                if (orderList.size() <= 0) {
                    // 查询结果为空
                    log.error("KhPayQueryTaskThread query orderList is null");
                    return;
                }

                File file = new File(filePath + "khPayBill-" + message.getOrderDate() + ".txt");
                if (!file.exists()) {
                    file.createNewFile();
                }
                fw = new FileWriter(file, true);
                fw.write(sumCount + "," + sumAmount + "\r\n");

                for (int i = 0; i < orderList.size(); i++) {
                    KeyedCollection orderDetail = (KeyedCollection) orderList.getElementAt(i);
                    fw.write(orderDetail.getDataValue("status") + "," + orderDetail.getDataValue("date") + ","
                            + orderDetail.getDataValue("charge") + "," + orderDetail.getDataValue("masterId") + ","
                            + orderDetail.getDataValue("orderId") + "," + orderDetail.getDataValue("currency") + ","
                            + orderDetail.getDataValue("amount") + "," + orderDetail.getDataValue("paydate") + ","
                            + orderDetail.getDataValue("settleflg") + "," + orderDetail.getDataValue("settletime")
                            + "," + orderDetail.getDataValue("chargeflg") + ","
                            + orderDetail.getDataValue("chargetime") + "\r\n");
                }
                fw.write("########" + "\r\n");
                fw.flush();
                fw.close();
                log.error("KhPayQueryTaskThread save file " + file.getName() + " success");
            } else {
                // 查询失败
                log.error("KhPayQueryTaskThread query error, errorCode: " + errorCode + " errorMsg: " + errorMsg);
            }
        } catch (Exception e) {
            log.error("KhPayQueryTaskThread error: ", e);
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    log.error("KhPayQueryTaskThread close error: ", e);
                }
            }
        }
    }

}
